<%-- 
    Document   : submission_page2.jsp
    Description: part 2 of the submission process. Takes users search parameters
    Created on : Mar 27, 2008, 11:59:34 AM
    Author     : Mark Bate
    
--%>


<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page session="true"%>
<%@page import="java.util.*" %>
<%@page import="pxgrid_controllers.*" %>
<%@page import="pxgrid_utils.*" %>
<%@page import="pxgrid_entities.*" %>
<%@page import="com.oreilly.servlet.*" %>
<%@page import="java.io.*" %>
<%@page import="java.net.*" %>


<%

//get a timestamp & append to end of tmp.
            String timestamp = String.valueOf(System.currentTimeMillis());
            String dirName = PXUtils.getDataStoreFromServerConfigXML() + "/tmp" + timestamp;

//create the folders
            File tempDir = new File(dirName);
            tempDir.mkdirs();

//grab the form as a mutlipart request
            MultipartRequest multi = new MultipartRequest(request, dirName, 20 * 1024 * 1024); // 10MB limit
            
            
//if Submit has been pressed, or Thunderbirds are FAB
            if (multi.getParameter("Submit").equals("Submit")) {
                //set the MTZFile
                String mtz_file = multi.getFilesystemName("MTZFile");
                //check it exists
                File mtz_file_test = new File(dirName + "/" + mtz_file);
                if (!mtz_file_test.isFile()) {
                    out.println("<html>");
                    out.println("<meta http-equiv=\"refresh\" content=\"0; url=error.jsp?errorcode=5\">");
                    out.println("</html>");
                    
                } else {

                    //Parse the MTZ File format, and grab the values we want from it

                    ArrayList meta_data = PXUtils.extractMetaDataFromMTZFile(dirName + "/" + mtz_file);
                    ArrayList fLabels = new ArrayList();
                    ArrayList sigfLabels = new ArrayList();
                    int space_group_id = 0;
                    String space_group_name = null;

                    for (int i = 0; i < meta_data.size(); i++) {
                        //grab the string
                        String meta_string = String.valueOf(meta_data.get(i));
                        
                        //check to see if column is of type F
                        if (meta_string.contains("  F  ")) {
                            String fString = meta_string.substring(7).trim();
                            int fString_end = fString.indexOf(" ");
                            fLabels.add(fString.substring(0, fString_end).trim());

                        }
                        //check to see if column is of type Q
                        else if (meta_string.contains("  Q  ")) {

                            String sigfString = meta_string.substring(7).trim();
                            int sigfString_end = sigfString.indexOf(" ");
                            sigfLabels.add(sigfString.substring(0, sigfString_end).trim());

                        }
                        //check to see if SYMINF column
                        else if (meta_string.contains("SYMINF")) {
                            String sgString = meta_string.substring(16).trim();
                            int sgString_end = sgString.indexOf(" ");
                            space_group_id = Integer.parseInt(sgString.substring(0, sgString_end).trim());

                        }
                    }
                    
                    //set the space_group_name from the space_group_id
                    switch(space_group_id) {
                        
                        case 1:
                            space_group_name = "P1";
                            break;
                        
                        case 3:
                            space_group_name = "P2";
                            break;
                        
                        case 4:
                            space_group_name = "P21";
                            break;
                            
                        case 5:
                            space_group_name = "C2";
                            break;
                            
                        case 16:
                            space_group_name = "P222";
                            break;
                            
                        case 17:
                            space_group_name = "P2221";
                            break;
                            
                        case 18:
                            space_group_name = "P21212";
                            break;
                            
                        case 19:
                            space_group_name = "P212121";
                            break;
                            
                        case 20:
                            space_group_name = "C2221";
                            break;
                            
                        case 21:
                            space_group_name = "C222";
                            break;
                            
                        case 22:
                            space_group_name = "F222";
                            break;
                            
                        case 23:
                            space_group_name = "I222";
                            break;
                            
                        case 24:
                            space_group_name = "I212121";
                            break;
                            
                        case 75:
                            space_group_name = "P4";
                            break;
                            
                        case 76:
                            space_group_name = "P41";
                            break;
                            
                        case 77:
                            space_group_name = "P42";
                            break;
                            
                        case 78:
                            space_group_name = "P43";
                            break;
                            
                        case 79:
                            space_group_name = "I4";
                            break;
                            
                        case 80:
                            space_group_name = "I41";
                            break;
                            
                        case 89:
                            space_group_name = "P422";
                            break;
                            
                        case 90:
                            space_group_name = "P4212";
                            break;
                            
                        case 91:
                            space_group_name = "P4122";
                            break;
                            
                        case 92:
                            space_group_name = "P41212";
                            break;
                            
                        case 93:
                            space_group_name = "P4222";
                            break;
                            
                        case 94:
                            space_group_name = "P42212";
                            break;
                            
                        case 95:
                            space_group_name = "P4322";
                            break;
                            
                        case 96:
                            space_group_name = "P43212";
                            break;
                            
                        case 97:
                            space_group_name = "I422";
                            break;
                            
                        case 98:
                            space_group_name = "I4122";
                            break;
                            
                        case 143:
                            space_group_name = "P3";
                            break;
                            
                        case 144:
                            space_group_name = "P31";
                            break;
                            
                        case 145:
                            space_group_name = "P32";
                            break;
                            
                        case 146:
                            space_group_name = "R3";
                            break;
                            
                        case 149:
                            space_group_name = "P312";
                            break;
                            
                        case 150:
                            space_group_name = "P321";
                            break;
                            
                        case 151:
                            space_group_name = "P3112";
                            break;
                        
                        case 152:
                            space_group_name = "P3121";
                            break;
                            
                        case 153:
                            space_group_name = "P3212";
                            break;
                            
                        case 154:
                            space_group_name = "P3221";
                            break;
                        
                        case 155:
                            space_group_name = "R32";
                            break;
                            
                        case 168:
                            space_group_name = "P6";
                            break;
                            
                        case 169:
                            space_group_name = "P61";
                            break;
                            
                        case 170:
                            space_group_name = "P65";
                            break;
                            
                        case 171:
                            space_group_name = "P62";
                            break;
                            
                        case 172:
                            space_group_name = "P64";
                            break;
                            
                        case 173:
                            space_group_name = "P63";
                            break;
                            
                        case 177:
                            space_group_name = "P622";
                            break;
                            
                        case 178:
                            space_group_name = "P6122";
                            break;
                            
                        case 179:
                            space_group_name = "P6522";
                            break;
                            
                        case 180:
                            space_group_name = "P6222";
                            break;
                            
                        case 181:
                            space_group_name = "P6422";
                            break;
                            
                        case 182:
                            space_group_name = "P6322";
                            break;
                        
                        case 195:
                            space_group_name = "P23";
                            break;
                            
                        case 196:
                            space_group_name = "F23";
                            break;
                            
                        case 197:
                            space_group_name = "I23";
                            break;
                            
                        case 198:
                            space_group_name = "P213";
                            break;
                            
                        case 199:
                            space_group_name = "I213";
                            break;
                            
                        case 207:
                            space_group_name = "P432";
                            break;
                            
                        case 208:
                            space_group_name = "P4232";
                            break;
                            
                        case 209:
                            space_group_name = "F432";
                            break;
                            
                        case 210:
                            space_group_name = "F4132";
                            break;
                            
                        case 211:
                            space_group_name = "I432";
                            break;
                            
                        case 212:
                            space_group_name = "P4332";
                            break;
                            
                        case 213:
                            space_group_name = "P4132";
                            break;
                            
                        case 214:
                            space_group_name = "I4132";
                            break;
                    }
                    
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <title>MR Grid Submission Page 2/2</title>
        <link href="styles/pxgrid_style.css" rel="stylesheet" type="text/css" />
        <script src="javascript/spacegroup.js" type="text/javascript"></script>
        
    </head>
    
    <body>
        <%@ include file="header_banner.html" %>
        <div id="page">
            <%@ include file="menubar.html" %>
            <!-- start content -->
            <div id="content">
                
            <p class="announcement">The file <% out.println(mtz_file);%> has been successfully uploaded.</p>    
            
            <div id="sampleFiles">
                <h2 class="sampleDataHeader">Looking for sample data?</h2>
                <div class="sampleDataImage">
                    <input type="button" value="Populate form" onClick="populateWithExampleValues();" class="inputButton">
                </div>
                <div class="sampleDataInstructions">
                    <p class="sampleDataInstructions">You can use this button to populate form fields with example data.
                    <br/>Then all you need to do is upload the example.zip file in the <a href="#tarUpload">"Upload structure factors"</a> section below.</p>
                </div>
                <br style="clear:both;" />

                <div class="sampleDataImage">
                    <a href="sampledata/example.zip"><img src="images/example_zip.gif" border="0"></a>
                </div>
                <div class="sampleDataInstructions">
                    <p class="sampleDataInstructions">This Zip archive contains the <a href="http://www.rcsb.org/" target="_blank">PDB</a> entries for <a href="http://www.rcsb.org/pdb/explore/explore.do?structureId=2I4A">2I4A</a> and <a href="http://www.rcsb.org/pdb/explore/explore.do?structureId=2O7K">2O7K</a>.
                        <br/>You should download the archive, then enter it in the <a href="#tarUpload">"Upload structure factors"</a> section below.
                    <br/><b>Note:</b> You may need to right-click the link and select "Download Linked File".</p>
                </div>
                <br style="clear:both;" />
                
                
            </div>
            
            <form id="phaserForm" name="phaserForm" method="POST" action="PXSubmitServlet" enctype="multipart/form-data">
                
                <!-- Information about the MTZ File -->
                <div id="MTZFormSection" class="formSection">
                    <div id="MTZFormSectionHeader" class="formSectionHeader">MTZ File Details</div>
                    <div id="MTZFormSectionContent" class="formSectionContent">
                        <!-- details of the previous page -->
                        <input type="hidden" name="tmpDir" value="<% out.println(dirName);%>">
                        <input type="hidden" name="MTZFile_uploaded" value="<% out.println(mtz_file);%>">
                        <p class="explanationText">F and SIGF Labels as derived from Structure Factors file.</p>
                        <%
        if (fLabels.size() == 1) {

            out.println("<p><span class=\"variableLabel\">F label:</span> " + fLabels.get(0));
            out.println("<input type=\"hidden\" name=\"flabel\" value=\"" + fLabels.get(0) + "\"></p>");

        }
        else {
            out.println("<div class=\"subsectionDiv\">");
            out.println("<p class=\"variableLabel\">F Label</p>");
            out.println("Multiple columns of type \"F\" were found within the MTZ file.<br/>");
            out.println("Please select the one you wish to use as your \"F label\":<br/><br/>");

            for (int i = 0; i < fLabels.size(); i++) {
                String fLabel = String.valueOf(fLabels.get(i));
                out.println("<input type=\"radio\" name=\"flabel\" value=\"" + fLabel + "\"");
                if (i == 0) {
                    out.println(" checked ");
                }
                out.println(">" + fLabel + "<br/>");
            }
            out.println("</div>");
        }
                        %>
                        <%
        if (sigfLabels.size() == 1) {

            out.println("<p><span class=\"variableLabel\">SIGF label:</span> " + sigfLabels.get(0));
            out.println("<input type=\"hidden\" name=\"sigflabel\" value=\"" + sigfLabels.get(0) + "\"></p>");

        }
        else {
            out.println("<div class=\"subsectionDiv\">");
            out.println("<p class=\"variableLabel\">SIGF Label</p>");
            out.println("Multiple columns of type \"Q\" were found within the MTZ file.<br/>");
            out.println("Please select the one you wish to use as your \"SIGF label\":<br/><br/>");


            for (int i = 0; i < sigfLabels.size(); i++) {
                String sigfLabel = String.valueOf(sigfLabels.get(i));
                out.println("<input type=\"radio\" name=\"sigflabel\" value=\"" + sigfLabel + "\"");
                if (i == 0) {
                    out.println(" checked ");
                }
                out.println(">" + sigfLabel + "<br/>");
            }
            out.println("</div>");
        }
                        %>
                        <p>
                            <span class="variableLabel">Sequence:</span>
                            
                            <p class="explanationText">Sequence is used to calculate molecular weight. It is not required if you already know the molecular weight of your protein.
                            <br/>Enter sequence in single-letter notation (Sequence only).
                            <br/>Eg. MKSEDSDLNKQ...</p>
                            <textarea id="sequence" onchange="calculateMW();" class="borderedInput" rows="5" cols="50"></textarea>
                            
                        </p>
                        <p>
                            <span class="variableLabel">Molecular Weight (Da):</span>&nbsp;
                            <input type="text" id="molWeight" name="molWeight" class="borderedInput" value="">
                        </p>
                        <p>
                            <span class="variableLabel">Number of Molecules in ASU:</span>&nbsp;
                            <input type="text" name="numberInAU" id="numberInAU" class="borderedInput" value="">
                        </p>
                        <div id="spaceGroupsDiv" class="subsectionDiv">
                            <p class="variableLabel">Space Groups</p>
                            <p class="explanationText">Select the space groups you wish to use.</p>
                            <input type="button" class="inputButton" name="selectAll" id="pgSelectAll" value="Select all" onClick="selectAllSpaceGroups(true)"/>
                            <input type="button" class="inputButton" name="deselectAll" id="pgDeselectAll" value="Deselect all" onClick="selectAllSpaceGroups(false)"/>                            
                            <br/>
                            <%
                                switch (space_group_id) {
                                    case 1:
                            %>
                            <%@ include file="spacegroups/triclinic.jsp" %>
                            <%
                                        break;
                                        
                                    case 3:
                                    case 4:
                            %>
                            <%@ include file="spacegroups/monoclinic_p.jsp" %>
                            <%
                                        break;
                                        
                                    case 5:        
                            %>
                            <%@ include file="spacegroups/monoclinic_c.jsp" %>
                            <%
                                        break;
                                        
                                    case 16:        
                                    case 17:
                                    case 18:
                                    case 19:
                            %>
                            <%@ include file="spacegroups/orthorhombic_p.jsp" %>
                            <%
                                        break;
                                        
                                    case 20:        
                                    case 21:
                            %>
                            <%@ include file="spacegroups/orthorhombic_c.jsp" %>
                            <%
                                        break;
                                        
                                    case 22:
                            %>
                            <%@ include file="spacegroups/orthorhombic_f.jsp" %>
                            <%
                                        break;
                                        
                                    case 23:        
                                    case 24:
                            %>
                            <%@ include file="spacegroups/orthorhombic_i.jsp" %>
                            <%
                                        break;
                                        
                                    case 75:        
                                    case 76:
                                    case 77:
                                    case 78:
                            %>
                            <%@ include file="spacegroups/tetragonal_p4.jsp" %>
                            <%
                                        break;
                                        
                                    case 79:        
                                    case 80:
                            %>
                            <%@ include file="spacegroups/tetragonal_i4.jsp" %>
                            <%
                                        break;
                                        
                                    case 89:        
                                    case 90:
                                    case 91:
                                    case 92:
                                    case 93:
                                    case 94:
                                    case 95:
                                    case 96:
                            %>
                            <%@ include file="spacegroups/tetragonal_p422.jsp" %>
                            <%
                                        break;
                                        
                                    case 97:        
                                    case 98:
                            %>
                            <%@ include file="spacegroups/tetragonal_i422.jsp" %>
                            <%
                                        break;
                                        
                                    case 143:        
                                    case 144:
                                    case 145:
                            %>
                            <%@ include file="spacegroups/trigonal_p3.jsp" %>
                            <%
                                        break;
                                        
                                    case 146:
                                    case 155:
                            %>
                            <%@ include file="spacegroups/trigonal_r.jsp" %>
                            <%
                                        break;
                                        
                                    case 149:        
                                    case 151:
                                    case 153:
                            %>
                            <%@ include file="spacegroups/trigonal_p312.jsp" %>
                            <%
                                        break;
                                        
                                    case 150:        
                                    case 152:
                                    case 154:
                            %>
                            <%@ include file="spacegroups/trigonal_p321.jsp" %>
                            <%
                                        break;
                                        
                                    case 168:        
                                    case 169:
                                    case 170:
                                    case 171:
                                    case 172:
                                    case 173:
                            %>
                            <%@ include file="spacegroups/hexagonal_p6.jsp" %>
                            <%
                                        break;
                                        
                                    case 177:        
                                    case 178:
                                    case 179:
                                    case 180:
                                    case 181:
                                    case 182:
                            %>
                            <%@ include file="spacegroups/hexagonal_p622.jsp" %>
                            <%
                                        break;
                                        
                                    case 195:        
                                    case 198:
                            %>
                            <%@ include file="spacegroups/cubic_p2.jsp" %>
                            <%
                                        break;
                                        
                                    case 196:
                            %>
                            <%@ include file="spacegroups/cubic_i2.jsp" %>
                            <%
                                        break;
                                        
                                    case 207:
                                    case 208:
                                    case 212:
                                    case 213:
                            %>
                            <%@ include file="spacegroups/cubic_p4.jsp" %>
                            <%
                                        break;
                                        
                                    case 209:        
                                    case 210:
                            %>
                            <%@ include file="spacegroups/cubic_f4.jsp" %>
                            <%
                                        break;
                                        
                                    case 211:        
                                    case 214:
                            %>
                            <%@ include file="spacegroups/cubic_i4.jsp" %>
                            <%
                                        break;
                                }
                            %>
                            <%@ include file="spacegroups/sgalternative_all.jsp" %>
                            <script type="text/javascript">
                                window.onload = selectSpaceGroup('<% out.print(space_group_name); %>');
                            </script>
                        </div>
                        
                    </div>
                </div>
                <div id="PDBFormSection" class="formSection">
                    <div id="PDBFormSectionHeader" class="formSectionHeader"><a name="tarUpload">PDB Search Models</a></div>
                    <div id="PDBFormSectionContent" class="formSectionContent">
                        <div class="subsectionDiv">
                            <p class="variableLabel">PDB Search Models</p>
                            <p class="explanationText">Please upload either a Zip or TAR archive containing the PDB files you wish to use as search ensembles:
                            <br/><b>Note:</b> MR Grid does not support archives that contain folders.</p>
                            <input type="file" name="PDBTar" id="PDBTar"/>
                            <br/>
                            <br/>
                            <h2 class="sampleDataHeader">How do I create a Zip archive?</h2>
                                    <p class="explanationText">Creating Zip archives in Mac OS X, is an quick and simple task.
                                    <br/>Simply select the files you wish to archive, then right-click (ctrl+click on single button mice) and select "Compress" (OS X 10.5) or "Create Archive" (OS X 10.4).
                                    <br/>This will create a new archive, which you can then upload.</p>
                        </div>
                        <p>
                            <span class="variableLabel">Packing:</span>&nbsp;
                            <input type="text" name="packing" id="packing" class="borderedInput" value="10">
                        </p>
                        <p>
                            <span class="variableLabel">Number of copies to search for:</span>&nbsp;
                            <input type="text" id="numberOfCopiesToSearchFor" name="numberOfCopiesToSearchFor" class="borderedInput">
                        </p>
                        
                        <div class="subsectionDiv">
                            <p class="variableLabel">RMSD Values:</p>
                            <p class="explanationText">Approximate difference between models and target structure
                            <br/><b>Note:</b> Phaser will only accept values between 0.8 and 2.6 inclusive.</p> 
                            <div id='rmsdDiv_1.5' >1.5&nbsp;
                                <input type='hidden' name='rmsdValue[]' value='1.5'>
                                <input type="button" name="rmRMSD" value="Remove" class="inputButton" onclick="removeRMSD('rmsdDiv_1.5');"/>
                                <br/><br/>
                            </div>
                            <div id="rmsdbox">
                            </div>    
                            
                            Add an RMSD Value:<input type="text" name="rmsdValue" id="rmsdValue" value="" class="borderedInput" />
                            
                            <input type="button" id="addRmsd" name="addRmsd" value="Add"
                                   onclick="addRMSD(document.getElementById('rmsdValue').value);" class="inputButton" />
                        </div>
                    </div>
                </div>
                <div id="PDBFormSection" class="formSection">
                    <div id="PDBFormSectionHeader" class="formSectionHeader">Grid Submission</div>
                    <div id="PDBFormSectionContent" class="formSectionContent">
                        <div class="subsectionDiv">
                            <p class="variableLabel">Available Grids:</p>
                            <p class="explanationText">Please select the grid you wish to use for this submission:</p>
                            <%
        File grid_file = new File("/mrconfig/xgrid_definitions.xml");

        if (!grid_file.isFile()) {
            out.println("<html>");
            out.println("<meta http-equiv=\"refresh\" content=\"0; url=error.jsp?errorcode=7\">");
            out.println("</html>");
            
        } else {
            
            Map xgrids_map = PXUtils.parseGridXML("/mrconfig/xgrid_definitions.xml");
        
            Iterator it = xgrids_map.values().iterator();
            int grid_count = 0;
            while (it.hasNext()) {

                PXGrid grid = (PXGrid) it.next();

                if(grid.getAvailable()) {
                    out.println("<input type=\"radio\" name=\"grid\" value=\""+grid.getDisplay_name()+"\"");
                    if(grid_count == 0) {
                        out.println(" checked");
                    }
                    out.println(">&nbsp;"+grid.getDisplay_name()+"<br/>");
                    grid_count++;
                }
            }
            
        }
                            %>
                        </div>
                        <br/>
                        <input type="reset" value="Reset" name="Reset" class="inputButton">
                        <input type="submit" value="Submit" name="Submit" class="inputButton">
                    </div>
                </div> 
                
            </form>
        </div>
        </div>
    </body>
</html>
<%
                }


            }
            else {

                out.println("<html>");
                out.println("<meta http-equiv=\"refresh\" content=\"0; url=unauthorized.jsp\">");
                out.println("</html>");
            }


%>

